$(function(){
    
    // Get the current users id from the URL
    var user_id = parseInt(getURLParameter("user"));
    
    getButtons(user_id);
    
    
    // Bind events to buttons
    $("#tNames").change(function(){$("#trackname").val($(this).val());});
    $("#button").button().on('click', function() {
        $("#dialog").dialog({
            title: "Upload new run",
            minWidth: 400,
            autoOpen: true,
            modal: true,
            stack: true,
            buttons: {
                Cancel: function() {
                    $(this).dialog("close");
                }
            }
        });
    });
    
    
    
    // Assign the current user to the upload action
    $("#upload-form").attr("action", "upload?user="+user_id);
    
   
    // Request the user data from the REST API and display some user meta data
    var user_req = getUser(user_id).done(function(user) {
        console.log(user);
        $("#username").prop('value', user.name);
        $("#password").prop('value', user.password);
        $("#avatar").prop('value', user.avatar);
        $("#Givenname").prop('value', user.givenName);
        $("#Familyname").prop('value', user.familyName);
        $("#email").prop('value', user.email);
        $("#editButton").button().on('click', function() {
            $("#dialog-edit").dialog({
                title: "Edit profile",
                minWidth: 400,
                autoOpen: true,
                modal: true,
                stack: true,
                buttons: {
                    Save: function() {
                        var newname = $("#username").val();
                        var newpass = $("#password").val();
                        var newavatar = $("#avatar").val();
                        var newgname = $("#Givenname").val()
                        var newfname = $("#Familyname").val();
                        var newemail = $("#email").val();
                        updateUser(user_id, newname, newpass, newavatar, newgname, newfname, newemail).done(function() {
                            window.location.href = '/webappz-gu/user.jsp?user=' + user_id;
                    }).fail(function(){
                        alert("fail");                   
                    });
                        $(this).dialog("close");
                    },
                    Cancel: function() {
                        $(this).dialog("close");
                    }
                }
            });
        });
        
        $("#welcome").text("Welcome, " + user.name);
        $("#status").append("user done! ");
        var presentation = "You've run a total of " + (user.totalDistance/1000).toFixed(2) + " km and for " 
                + (user.totalTime/3600).toFixed(2) + " hours. Good job!";
        $("#presentation").append(presentation);
    });
    
    // Get the users tracks and runs from the REST API and display them
    getTracks(user_id).done(function(tracks) {
        var tnames = "<option class='ui-corner-all'>None</option>";
        
        $("track_list").append("<ul>");
        for(var i = 0; i < tracks.length; i++){
            var track_name = tracks[i];
            
            tnames += "<option value='" + track_name + "' class='ui-corner-all'>" + track_name + "</option>";
            
            // Get all runs from the current track
            getRunsByTrack(user_id, track_name).done(function(runs) {
                var url = "track.jsp?user="+user_id+"&track=" + escape(runs[0].trackName);
                var runs_html = "<li><a href='"+url+"'>"+runs[0].trackName+"</a>";
                
                for(var j = 0; j < runs.length; j++){
                    runs_html += "<ul>";
                    var date = new Date(runs[j].runDate);
                    runs_html += "<li><a href='run.jsp?user="+user_id+"&run="+runs[j].id+"'>" + date.toUTCString() + "</a></li>";
                    runs_html += "</ul></li>";
                }
                $("#track_list").append(runs_html);
                
            });
        }
        $("#tNames").append(tnames);
        $("#track_list").html("</ul>");
    });
});
